package com.vertx.firstvertx;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamTest {

	public static void main(String[] args) {

		String str = "My name is Walter";

		//Stream.of(str.split(" ")).map(s -> s.length()).filter(s -> s > 2).forEach(System.out::println);

		//flatMap A -> B属性(B是一个集合)，最终得到所有的A元素里面的所有B属性集合
		//intStream/longStream并不是Stream的自雷，所以要进行装箱 boxed
//    Stream.of(str.split(" ")).flatMap(s -> s.chars().boxed()).forEach(
//      i -> System.out.println((char)i.intValue())
//    );

		//peek用于debug，是中间操作，forEach是终止操作
		//Stream.of(str.split(" ")).peek(System.out::println).forEach(System.out::println);

		//Stream.of(str.split(" ")).max((s1, s2) -> s1.length() - s2.length() );

		IntStream.range(1, 100).peek(StreamTest::debug1).count();

		//Map<String,String> map = new HashMap<>();
//    ForkJoinPool pool = new ForkJoinPool(20);
//    pool.submit(() -> IntStream.range(1,100).parallel().peek(StreamTest::debug1).count());
//    pool.shutdown();
//
//    synchronized (pool){
//      try {
//        pool.wait();
//      } catch (InterruptedException e) {
//        e.printStackTrace();
//      }
//    }

		Random random = new Random();

		Stream<Integer> stream = Stream.generate(() -> random.nextInt())
				.limit(500)
				.peek(s -> System.out.println("peek:" + s))
				.filter(s -> {
					System.out.println("filter:" + s);
					return s > 1000000;
				});

		stream.count();

	}

	public static void debug1(int i) {
		System.out.println("debug1:" + i);
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
